package com.qbw.app.module.common.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.camera2.CameraManager;

import androidx.annotation.NonNull;

import com.lib.util.ResUtil;
import com.qbw.app.R;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * @author qinbaowei
 * @date 2018/2/8
 * @email qbaowei@qq.com
 */

public class FlashLightUtil implements EasyPermissions.PermissionCallbacks {
    private Activity mActivity;
    private CameraManager mManager;// 声明CameraManager对象
    private Camera mCamera;// 声明Camera对象
    private boolean mLightOn;
    private boolean mCameraFromOtherPlace = false;

    public FlashLightUtil() {
    }

    public FlashLightUtil(Camera camera) {
        mCamera = camera;
        mCameraFromOtherPlace = true;
    }

    public void tryLightSwitch(Activity context, boolean lightOn) {
        if (lightOn) {
            String[] perms = new String[]{Manifest.permission.CAMERA};
            if (EasyPermissions.hasPermissions(context, perms)) {
                lightSwitch(context, lightOn);
            } else {
                EasyPermissions.requestPermissions(context,
                                                   ResUtil.getString(R.string.rotional_camera),
                                                   0,
                                                   perms);
            }
        } else {
            lightSwitch(context, false);
        }
    }

    /**
     * 手电筒控制方法
     *
     * @param lightOn
     * @return
     */
    private void lightSwitch(Activity context, boolean lightOn) {
        if (lightOn) {

            mActivity = context;
            if (/*Build.VERSION.SDK_INT >= Build.VERSION_CODES.M*/false) {

                try {
                    mManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
                    mManager.setTorchMode("0", true);
                    mLightOn = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                final PackageManager pm = context.getPackageManager();
                final FeatureInfo[] features = pm.getSystemAvailableFeatures();
                for (final FeatureInfo f : features) {
                    if (PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) { // 判断设备是否支持闪光灯
                        if (null == mCamera) {
                            mCamera = Camera.open();
                        }
                        final Camera.Parameters parameters = mCamera.getParameters();
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        mCamera.setParameters(parameters);
                        mCamera.startPreview();
                        mLightOn = true;
                    }
                }
            }
        } else {
            mLightOn = false;
            mActivity = null;

            if (/*Build.VERSION.SDK_INT >= Build.VERSION_CODES.M*/false) {
                try {
                    if (mManager == null) {
                        return;
                    }
                    mManager.setTorchMode("0", false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                if (mCamera != null) {
                    if (mCameraFromOtherPlace) {
                        final Camera.Parameters parameters = mCamera.getParameters();
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                        mCamera.setParameters(parameters);
                    } else {
                        mCamera.stopPreview();
                        mCamera.release();
                        mCamera = null;
                    }
                }
            }
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        lightSwitch(mActivity, true);
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {

    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    public boolean isLightOn() {
        return mLightOn;
    }
}
